/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	WindowingSystem.h
*
*	Description -	System for handling physical windows.
*
*	Comments	-	
*
*	Modification History:
*			Name			   Date					Description
*			MrCodeSushi	-	04/08/2013	-	Creation of this file
**************************************************************************************/
#ifndef _PSX_WINDOWING_SYSTEM_H_
#define _PSX_WINDOWING_SYSTEM_H_

#include "PulseSTD.h"
#include "ISystem.h"
#include "Array.h"
#include "Window.h"
#include "WindowView.h"
#include "EventSystem.h"

namespace Pulse
{
	class WindowingSystem : public ISystem
	{
	public:

		WindowingSystem( void );

		virtual ~WindowingSystem( void );

		virtual EErrorCode::Type Initialize( void );

		virtual void Shutdown( void );

		EErrorCode::Type CreateWindow_( const CHAR *pWindowName, Window **ppOut );

		const SIZE_T GetNumWindows( void ) const { return m_windows.GetSize(); }

		void RemoveWindow( const CHAR *pWindowName );

		void RemoveWindow( const UPOS_T index );

		void RemoveAllWindows( void ) { ClearWindows(); }

		Window * FindWindow_( const CHAR *pWindowName );

		EErrorCode::Type CreateWindowView( const CHAR *pViewName, WindowView **ppOut );

		WindowView * FindWindowView( const CHAR *pViewName );

		EErrorCode::Type RemoveWindowView( const CHAR *pWindowViewName );

		void Update( const FLOAT dt );

		void FixedUpdate( const FLOAT dt ); // Useful for accurate numerical calculations like the Physics system

		void Render( void );

		EventSystem * GetEventSystem( void ) { return &m_eventDispatcher; }

	private:

		void ClearWindowViews( void );

		void ClearWindows( void );

		void DeleteWindow( Window *pWindow );

		void DeleteWindowView( WindowView *pView );

	private:

		// Input system delegates
		//////////////////////////

		void OnKeyUp( IEvent *pEvent );
		
		void OnKeyDown( IEvent *pEvent );
		
		void OnMouseMove( IEvent *pEvent );
		
		void OnMouseButtonUp( IEvent *pEvent );
		
		void OnMouseButtonDown( IEvent *pEvent );

		// Window Gain/Lose Focus Events
		/////////////////////////////////

		void OnWindowGainFocus( IEvent *pEvent );
		
		void OnWindowLoseFocus( IEvent *pEvent );

	private:

		typedef Array<Window*> WindowList;
		typedef Array<WindowView*> ViewList;

		WindowList	m_windows;
		ViewList	m_views;

		Window *m_pActiveWindow;

		EventSystem m_eventDispatcher;
	};
}

#endif /* _PSX_WINDOWING_SYSTEM_H_ */